package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.Exception.BusinessException;
import com.qk.commen.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> list(UserDto userDto) {

        //设置分页
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        //调用mapper
        List<User> users = userMapper.selectList(userDto);
        //解析分页结果,强制转
        Page<User> page = (Page<User>) users;
        return new PageResult<>(page.getTotal(), page.getResult());

    }

    @Override
    public void delete(List<Integer> ids) {
        userMapper.deleteUserRole(ids);
    }

    /**
     * 添加用户
     */
    @Override
    public void add(User user) {
        //补全
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //设置密码
//        user.setPassword(user.getUsername()+"123");

        String psssword = user.getUsername() + "123";
        user.setPassword(DigestUtils.md5DigestAsHex(psssword.getBytes()));
        userMapper.insert(user);
    }

    /**
     * select user by id
     */
    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);

    }

    /**
     * update user
     */
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);


    }

    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }

    @Override
    public List<User> listByRole(String roleLabel) {

        return userMapper.listByRole(roleLabel);


    }

    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);

    }

    @Override
    public LoginResultVo login(User user) {
        //1调用mapper,根据用户名一个值查询用户所有信息
        User u = userMapper.selectUsername(user.getUsername());
        //        2,判断用户是否存在
        if (u == null) {
            throw new BusinessException("用户名或密码错误");
        }
        // .判断密码
        String _pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!_pass.equals(u.getPassword())) {
            throw new BusinessException("用户名或密码错误");
        }
        //判断状态
        if (u.getStatus() == 0) {
//         return null;
            throw new BusinessException("用户被停用");
        }
//        4.封装返回
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setName(u.getName());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());

        //调用工具类
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        return loginResultVo;
    }
}
